package com.new_wear.project.system.vehicleinformation.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.system.vehicleinformation.domain.Vehicleinformation;
import com.new_wear.project.system.vehicleinformation.service.IVehicleinformationService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;

/**
 * 车辆信息Controller
 * 
 * @author new_wear
 * @date 2019-12-23
 */
@Controller
@RequestMapping("/system/vehicleinformation")
public class VehicleinformationController extends BaseController
{
    private String prefix = "system/vehicleinformation";

    @Autowired
    private IVehicleinformationService vehicleinformationService;

    @RequiresPermissions("system:vehicleinformation:view")
    @GetMapping()
    public String vehicleinformation()
    {
        return prefix + "/vehicleinformation";
    }

    /**
     * 查询车辆信息列表
     */
    @RequiresPermissions("system:vehicleinformation:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Vehicleinformation vehicleinformation)
    {
        startPage();
        List<Vehicleinformation> list = vehicleinformationService.selectVehicleinformationList(vehicleinformation);
        if (list != null){
            for (Vehicleinformation onlist :list) {
                if (onlist.getState() == 0){
                    onlist.setReserve2("正常");
                }else if (onlist.getState() == 1){
                    onlist.setReserve2("报废");
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出车辆信息列表
     */
    @RequiresPermissions("system:vehicleinformation:export")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Vehicleinformation vehicleinformation)
    {
        List<Vehicleinformation> list = vehicleinformationService.selectVehicleinformationList(vehicleinformation);
        ExcelUtil<Vehicleinformation> util = new ExcelUtil<Vehicleinformation>(Vehicleinformation.class);
        return util.exportExcel(list, "vehicleinformation");
    }

    /**
     * 新增车辆信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆信息
     */
    @RequiresPermissions("system:vehicleinformation:add")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Vehicleinformation vehicleinformation)
    {
        return toAjax(vehicleinformationService.insertVehicleinformation(vehicleinformation));
    }

    /**
     * 修改车辆信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Vehicleinformation vehicleinformation = vehicleinformationService.selectVehicleinformationById(id);
        if (vehicleinformation != null){

                if (vehicleinformation.getState() == 0){
                    vehicleinformation.setReserve2("正常");
                }else if (vehicleinformation.getState() == 1){
                    vehicleinformation.setReserve2("报废");
                }
        }
        mmap.put("vehicleinformation", vehicleinformation);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆信息
     */
    @RequiresPermissions("system:vehicleinformation:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Vehicleinformation vehicleinformation)
    {
        return toAjax(vehicleinformationService.updateVehicleinformation(vehicleinformation));
    }

    /**
     * 删除车辆信息
     */
    @RequiresPermissions("system:vehicleinformation:remove")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(vehicleinformationService.deleteVehicleinformationByIds(ids));
    }
}
